<?php
class MsgsendAction extends CommonAction{
    function notify_msg_list(){
        $deal_id = intval(M('deal_notify')->field('deal_id')->order('create_time asc')->limit(1)->find());
        if($deal_id>0)
        {
            $deal_info = M('deal')->where(array('id'=>$deal_id))->find();
            //一次性生成50条
            $deal_notify_items = M('user_deal_notify')->where(array('deal_id'=>$deal_id))->order('create_time asc')->limit(50)->select();
            $ids = array(0);
            foreach($deal_notify_items as $k=>$v)
            {
                $ids[] = $v['id'];
            }
            $affected_rows = M('user_deal_notify')->where(array('id'=>array('IN',$ids)))->delete();
            if($affected_rows==count($deal_notify_items))
            {
                $deal_count = intval(M('user_deal_notify')->where(array('deal_id'=>$deal_id))->count());
                if($deal_count==0){
                    M('deal_notify')->where(array('deal_id'=>$deal_id))->delete();
                }
                //开始发送通知
                foreach ($deal_notify_items as $k=>$v)
                {
                    send_notify($v['user_id'],$deal_info['name']." 在 ".to_date($deal_info['success_time'])." 成功筹到 ".format_price($deal_info['limit_price']),"deal#show","id=".$deal_info['id']);
                }
            }
            header("Content-Type:text/html; charset=utf-8");
            echo 1;
        }
        else
        {
            header("Content-Type:text/html; charset=utf-8");
            echo 0;
        }
    }

    function deal_msg_list(){
        //业务队列的群发
        $affected_rows = M('conf')->where(array('name'=>'DEAL_MSG_LOCK','value'=>'0'))->data(array('value'=>'1'))->save();
        if($affected_rows)
        {
            $msg_item = M('deal_msg_list')->where(array('is_send'=>'0'))->order('id asc')->limit(1)->find();
            if($msg_item)
            {
                //优先改变发送状态,不论有没有发送成功
                M('deal_msg_list')->where(array('id'=>intval($msg_item['id'])))->data(array('is_send'=>'1','send_time'=>get_gmtime()))->save();
                if($msg_item['send_type']==0)
                {
                    //短信
                    require_once SITE_PATH."system/utils/es_sms.php";
                    $sms = new sms_sender();
                    $result = $sms->sendSms($msg_item['dest'],$msg_item['content']);
                    //发送结束，更新当前消息状态
                    M('deal_msg_list')->where(array('id'=>intval($msg_item['id'])))->data(array('is_success'=>(string)intval($result['status']),'result'=>$result['msg']))->save();
                }

                if($msg_item['send_type']==1)
                {
                    //邮件
                    require_once SITE_PATH."system/utils/es_mail.php";
                    $mail = new mail_sender();

                    $mail->AddAddress($msg_item['dest']);
                    $mail->IsHTML($msg_item['is_html']); 				  // 设置邮件格式为 HTML
                    $mail->Subject = $msg_item['title'];   // 标题
                    $mail->Body = $msg_item['content'];  // 内容

                    $is_success = $mail->Send();
                    $result = $mail->ErrorInfo;

                    //发送结束，更新当前消息状态
                    M('deal_msg_list')->where(array('id'=>intval($msg_item['id'])))->data(array('is_success'=>(string)intval($is_success),'result'=>$result))->save();
                }
            }
            header("Content-Type:text/html; charset=utf-8");
            echo intval(M('deal_msg_list')->where(array('is_send'=>'0'))->count());
            M('conf')->where(array('name'=>'DEAL_MSG_LOCK'))->data(array('value'=>'0'))->save();
        }
        else
        {
            header("Content-Type:text/html; charset=utf-8");
            echo 0;
        }
    }
}